package algorithm;

/**
 * 二分查找非递归实现
 */
public class BinarySearchNoRecur {
    public static void main(String[] args) {
        int [] arr={1,3,5,7,9,10};
        System.out.println(binarySearch(arr,3));
    }

    public static int binarySearch(int arr[], int target) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] == target) {
                return mid;
            } else if (arr[mid] > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return -1;

    }
}

